package com.example.lenovo.sbns.Activity;


import android.app.Fragment;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lenovo.sbns.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class EnterFragment extends Fragment {
    private Button register, forget, enter;
    private EditText UseName, UsePass;

    MySQlite mySQlite;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        System.out.println("07");
        View view = inflater.inflate(R.layout.fragment_photo_page, container, false);
        System.out.println("06");

        UseName = (EditText) view.findViewById(R.id.UseName);
        UsePass = (EditText) view.findViewById(R.id.UsePass);
        mySQlite = new MySQlite(getActivity(), "sbns_db", null, 1);

        //注册按钮的监听
        register = (Button) view.findViewById(R.id.RegisterButton);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), RegisterPage.class);
                startActivity(intent);
            }
        });
        //监听完成
        //忘记密码的监听
        forget = (Button) view.findViewById(R.id.ForgetButton);
        forget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ForgetPage.class);
                startActivity(intent);
            }
        });
        //忘记密码监听完成
        //登入按钮监听
        enter = (Button) view.findViewById(R.id.EnterButton);
        enter.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                if (UseName.getText().toString().equals("") || UsePass.getText().toString().equals("")) {
                    Toast.makeText(getActivity().getApplicationContext(), "请输入账号密码！",
                            Toast.LENGTH_SHORT).show();
                } else {


                    try {
                        SQLiteDatabase db = mySQlite.getReadableDatabase();
                        Cursor cursor = db.rawQuery("select * from sbns_db where name=? and permisson=?",
                                new String[]{UseName.getText().toString(),
                                        UsePass.getText().toString()});
                        if (cursor.getCount() > 0) {
                            Intent intent = new Intent(getActivity(), indexPage.class);
                            Bundle bundle = new Bundle();
                            bundle.putString("name", UseName.getText().toString());
                            intent.putExtras(bundle);
                            startActivity(intent);
                        } else {
                            Toast.makeText(getActivity().getApplicationContext(), "登入失败",
                                    Toast.LENGTH_SHORT).show();
                        }
                        cursor.close();
                        db.close();
                    } catch (SQLiteException e) {
                        Toast.makeText(getActivity().getApplicationContext(), "亲，请注册！",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });


        //登入按钮监听完成

        return view;

    }
    }



